﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ParameterProjectSpaceList.h"
#include "IParameterValueStorage.h"
#include "IParameterDefinitionLibrary.h"
#include "IDatasetManager.h"
#include "IDataset.h"
#include <vector>
#include "ICollaborationSpaceConfiguration.h"
#include "ICollaborationSpaceConfigurationItem.h"
#include "IParameterValueString.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace gcmp;


DATA_DEFINE(ParameterProjectSpaceList)
{
}

gcmp::ParameterProjectSpaceList::ParameterProjectSpaceList(const UniIdentity& uid) :
GbmpParameterValueListBase(uid)
{
}

OwnerPtr<IParameterValueList> gcmp::ParameterProjectSpaceList::Clone() const
{
    return NEW_AS_OWNER_PTR(ParameterProjectSpaceList, *this);
}

std::wstring gcmp::ParameterProjectSpaceList::OnGetDisplayStringByValue( const IDocument* pDoc, const IParameterValueStorage* pValue ) const
{
    std::wstring res;

    DBG_WARN_AND_RETURN_UNLESS(pDoc, res, L"无效的参数pDoc",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_UNLESS(pValue, res, L"无效的参数pValue",L"GDMPLab",L"2024-03-30");

    const IParameterValueString* pFloorId = dynamic_cast<const IParameterValueString*>(pValue);
    DBG_WARN_AND_RETURN_UNLESS(pFloorId, res, L"pFloorId为空",L"GDMPLab",L"2024-03-30");

//     ElementId id = pModelId->GetValue();
//     res += GBMP_TR(L"：") + pFamilyType->GetBasicInformation()->GetName();
    const IDataset* pDataset = IDatasetManager::Get()->GetDataset(pDoc);

    // 这是“空”的选项
    if(!pDataset)
    {
        res = s_nullName;
    }
    else
    {
        const std::wstring floorid = pFloorId->GetValue();
        const ICollaborationProjectConfiguration* pCollaborationProjectConfiguration = pDataset->GetProjectConfiguration(CollaborationProjectConfigurationType::Space);
        if (!pCollaborationProjectConfiguration)
        {
            res = s_nullName;
            return res;
        }
        const ICollaborationSpaceConfiguration* pCollaborationSpaceConfiguration = dynamic_cast<const ICollaborationSpaceConfiguration*>(pCollaborationProjectConfiguration);
        if (!pCollaborationSpaceConfiguration)
        {
            res = s_nullName;
            return res;
        }
        const ICollaborationSpaceConfigurationItem* pFloorItem = pCollaborationSpaceConfiguration->GetConfigurationItem(floorid);
        if (!pFloorItem)
        {
            res = s_nullName;
            return res;
        }
        res = pFloorItem->GetName();
    }

    return res;
}

std::vector<OwnerPtr<IParameterValueStorage>> gcmp::ParameterProjectSpaceList::OnGetValues(const IDocument* pDoc, const std::vector<ElementId>& ids/* = std::vector<ElementId>()*/) const
{
    std::vector<OwnerPtr<IParameterValueStorage>> res;
    DBG_WARN_AND_RETURN_UNLESS(pDoc, res, L"无效的参数pDoc",L"GDMPLab",L"2024-03-30");

    const IDataset* pDataset = IDatasetManager::Get()->GetDataset(pDoc);
    if (!pDataset)
    {
        res.emplace_back(IParameterValueString::Create(L""));
    }
    else
    {
        const ICollaborationProjectConfiguration* pCollaborationProjectConfiguration = pDataset->GetProjectConfiguration(CollaborationProjectConfigurationType::Space);
        if (!pCollaborationProjectConfiguration) 
        {
            res.emplace_back(IParameterValueString::Create(L""));
            return res;
        }
        const ICollaborationSpaceConfiguration* pCollaborationSpaceConfiguration = dynamic_cast<const ICollaborationSpaceConfiguration*>(pCollaborationProjectConfiguration);
        if (!pCollaborationSpaceConfiguration)
        {
            res.emplace_back(IParameterValueString::Create(L""));
            return res;
        }

        //site
        std::vector<const ICollaborationSpaceConfigurationItem*> CollaborationSpaceConfigurationSites = pCollaborationSpaceConfiguration->GetFirstLevelConfigurationItems();
        if (CollaborationSpaceConfigurationSites.size()==0)
        {
            res.emplace_back(IParameterValueString::Create(L""));
            return res;
        }
        FOR_EACH(CollaborationSpaceConfigurationSite, CollaborationSpaceConfigurationSites)
        {
            std::vector<const ICollaborationSpaceConfigurationItem*> CollaborationSpaceConfigurationBuildings = CollaborationSpaceConfigurationSite->GetSubItems();
            FOR_EACH(CollaborationSpaceConfigurationBuilding, CollaborationSpaceConfigurationBuildings)
            {
                std::vector<const ICollaborationSpaceConfigurationItem*> CollaborationSpaceConfigurationRegions = CollaborationSpaceConfigurationBuilding->GetSubItems();
                FOR_EACH(CollaborationSpaceConfigurationRegion, CollaborationSpaceConfigurationRegions)
                {
                    std::vector<const ICollaborationSpaceConfigurationItem*> CollaborationSpaceConfigurationFloors = CollaborationSpaceConfigurationRegion->GetSubItems();
                    FOR_EACH(CollaborationSpaceConfigurationFloor, CollaborationSpaceConfigurationFloors)
                    {
                        res.emplace_back(IParameterValueString::Create(CollaborationSpaceConfigurationFloor->GetId()));
                    }
                }
            }
        }
    }
    return res;
}
